package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import model.Class_NIIT;
import model.CommonList;
import model.RentCost;
import model.RentPrice;

public class DaoImp_Rent implements IDao_Rent
{
	private DaoMethod method;
	private Connection conn;
	private CommonList common;

	public DaoImp_Rent()

	{
		method = new DaoMethod();
		conn = DaoMethod.getConnection();
		common = CommonList.getInstance();
	}

	public int setRent(RentPrice rentPrice)
	{
		String exe = "insert into rentprice(rentPerMonth,date_modify)values(?,?)";
		ArrayList<Object> param = new ArrayList<Object>();
		param.add(rentPrice.getRentPerMonth());
		param.add(rentPrice.getDate_modify());
		return method.pstUpdate(exe, param);
	}

	public List<RentCost> queryByName(String name)
	{
		String exe = "select id,name,number,date_in,teacher,sum from rentcost_view where name='"
				+ name + "' and isout=0 order by teacher";
		return getRentCostList(exe);
	}

	public List<RentCost> queryByTeacherName(String teacherName)
	{
		String exe = "select id,name,number,date_in,teacher,sum from rentcost_view where teacher='"
				+ teacherName + "' and isout=0 order by teacher";
		return getRentCostList(exe);
	}

	public List<RentCost> queryAll()
	{
		String exe = "select id,name,number,date_in,teacher,sum from rentcost_view where isout=0 order by teacher";
		return getRentCostList(exe);
	}

	private List<RentCost> getRentCostList(String exe)
	{
		Statement st = null;
		ResultSet rs = null;
		List<RentCost> list = new ArrayList<RentCost>();
		try
		{
			st = conn.createStatement();
			rs = st.executeQuery(exe);
			if (rs.next())
			{
				do
				{
					RentCost rentCost = new RentCost();
					rentCost.setId(rs.getInt(1));
					rentCost.setName(rs.getString(2));
					rentCost.setDormNumber(rs.getString(3));
					rentCost.setDate_in(rs.getDate(4));
					rentCost.setTeacher_name(rs.getString(5));
					rentCost.setAlreadyPay(rs.getInt(6));
					list.add(rentCost);
				}
				while (rs.next());
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
			}
			catch (SQLException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try
			{
				if (st != null)
				{
					st.close();
				}
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return list;
	}

	public RentCost getInitRentCost(String name)
	{
		String exe1 = "select id,name,number,date_in,teacher,sum from rentcost_view order by teacher";
		String exe = "select s.id,d.number,s.date_in,t.teacher_name from student s,dorm d,teacher t,class c where s.dorm_id=d.id and s.class_id=c.id and c.teacher_id=t.id and s.isout=0 and s.name='"
				+ name + "'";
		Vector<Vector<Object>> vector = method.stQuery(exe);

		if (vector.isEmpty())
		{
			return null;
		}
		else
		{
			Vector<Object> vec = vector.get(0);
			RentCost rentCost = new RentCost();
			rentCost.setId(Integer.parseInt(vec.get(0).toString()));
			rentCost.setName(name);
			rentCost.setDormNumber(vec.get(1).toString());
			rentCost.setDate_in(Date.valueOf(vec.get(2).toString()));
			rentCost.setTeacher_name(vec.get(3).toString());
			rentCost.setAlreadyPay(0);
			return rentCost;
		}

	}

	public Properties getProperties()
	{
		return DaoMethod.getProperties();
	}
}
